package com.huang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huang.entity.RecommendProduct;
import com.huang.mapper.DiscountProductMapper;
import com.huang.mapper.RecommendProductMapper;
import com.huang.mapper.SingleProductMapper;
import com.huang.untils.PupuCrawler;
import com.huang.untils.ReCode;
import com.huang.untils.ReStates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author huang
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/product")
public class RecommendProductController {


    private BaseMapper baseMapper;

    private RecommendProductMapper productMapper;

    private SingleProductMapper singleProductMapper;

    private DiscountProductMapper discountProductMapper;

    @Autowired
    public void setSingleProductMapper(SingleProductMapper singleProductMapper) {
        this.singleProductMapper = singleProductMapper;
    }

    @Autowired
    public void setDiscountProductMapper(DiscountProductMapper discountProductMapper) {
        this.discountProductMapper = discountProductMapper;
    }

    private PupuCrawler pupuCrawler;

    @Autowired
    public void setPupuCrawler(PupuCrawler pupuCrawler) {
        this.pupuCrawler = pupuCrawler;
    }

    @Autowired
    public void setProductMapper(RecommendProductMapper productMapper) {
        this.productMapper = productMapper;
    }


    @GetMapping("/data/recommend")
    private ReCode getRecommendData() {
        System.out.println(1111);
        List recommendProducts = productMapper.selectList(null);
        return new ReCode().isOK().put(recommendProducts);
    }


    @GetMapping("/data/Discount")
    private ReCode getDiscountData() {
        List recommendProducts = discountProductMapper.selectList(null);
        return new ReCode().isOK().put(recommendProducts);
    }

    @GetMapping("/data/Single")
    private ReCode getSingleData() {
        return new ReCode().isOK().put(singleProductMapper.selectList(null));
    }


    @RequestMapping("/data/{type}/pname={name}")
    private ReCode getLikeData(@PathVariable("name") String name, @PathVariable("type")  String type) {

        seleteType(type);

        QueryWrapper<RecommendProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        List list = baseMapper.selectList(queryWrapper);
        System.out.println( list);
        return new ReCode().isOK().put(list);
    }



    @GetMapping("/data/refresh={type}")
    private ReCode reFlashPupuData(@PathVariable String type) {
        System.out.println(type);
        pupuCrawler.contextLoads(type);
        return new ReCode().isOK();
    }

    @GetMapping("/data/{type}/deleteId={id}")
    private ReCode deleteData(@PathVariable String id, @PathVariable String type){

        System.out.println(type+"11111");
        seleteType(type);
        Map<String,Object> map=new HashMap<>();
        map.put("product_id",id);
        System.out.println("11");
        baseMapper.deleteByMap(map);
        return new ReCode().isOK();
    }

    private void seleteType(String type){

        switch (type) {
            case "recommend":
                baseMapper = productMapper;
                break;
            case "Discount":
                baseMapper = discountProductMapper;
                break;
            case "Single":
                baseMapper = singleProductMapper;
                break;
        }
    }

}


